package com.cdivtc.management;

import com.cdivtc.management.service.JwtFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

//核心类
@Configuration
public class ShiroConfigUration {
    //
    @Bean("shiroFillter")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manger) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(manger);
        bean.setLoginUrl("/login");
        bean.setSuccessUrl("/index");
        bean.setUnauthorizedUrl("/unauthorized");
        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filterMap = new HashMap<>();
        //设置我们自定义的JWT过滤器
        filterMap.put("jwt", new JwtFilter());
        bean.setFilters(filterMap);
        LinkedHashMap<String, String> fillChainDeefinitionMap = new LinkedHashMap<>();
        fillChainDeefinitionMap.put("/index", "jwt");
        fillChainDeefinitionMap.put("/login", "jwt");
        fillChainDeefinitionMap.put("/queryallcollage","jwt");
        fillChainDeefinitionMap.put("/loginUser", "jwt");
        fillChainDeefinitionMap.put("/achievement/queryAchBySchool", "jwt[president,dean]");
        fillChainDeefinitionMap.put("/achievement/queryAchByCollage/**", "jwt[dean]");
        fillChainDeefinitionMap.put("/**", "jwt");

        bean.setFilterChainDefinitionMap(fillChainDeefinitionMap);
        return bean;

    }


    @Bean("securityManager")
    public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm realm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
       
        return manager;
    }


    @Bean("authRealm")
    public AuthRealm authRealm(@Qualifier("credentialMatcher") CredentialMatcher matcher) {
        AuthRealm authRealm = new AuthRealm();
        authRealm.setCredentialsMatcher(matcher);
        authRealm.setCachingEnabled(true);
        authRealm.setAuthorizationCachingEnabled(true);
        return authRealm;
    }

    @Bean("credentialMatcher")
    public CredentialMatcher credentialMatcher() {
        return new CredentialMatcher();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager manager) {

        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(manager);
        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);


        return creator;
    }

}
